package cc.mxtt.mvc.model;

import java.util.ArrayList;
import java.util.List;

import cc.mxtt.common.utils.StringUtil;

import com.jfinal.plugin.activerecord.Model;

public class Power extends Model<Power>{
	public static final Power dao = new Power();
	
	public List<PowerMenu> getTree(String role_id, List<Power> parentList){
		List<PowerMenu> listTreeMenu = new ArrayList<PowerMenu>();
		for(Power menu:parentList){
			PowerMenu treeMenu = new PowerMenu();
			treeMenu.setId(StringUtil.getString(menu.getAttrs().get("id")));
			treeMenu.setText(StringUtil.getString(menu.getAttrs().get("text")));
			if(StringUtil.getString(menu.getAttrs().get("checked")).equals("true")){
				treeMenu.setChecked(true);
			}else{
				treeMenu.setChecked(false);
			}
			treeMenu.setChildren(getTree(role_id,getChildrenTree(role_id, StringUtil.getString(menu.getAttrs().get("id")))));
			listTreeMenu.add(treeMenu);
		}
		return listTreeMenu;
	}
	
	
		public List<Power> getChildrenTree(String role_id, String parent_menu_id){
			String sql = "select * from (";
			sql = sql + " select t.id,t.name text,IF(ISNULL(r.role_id)=0,'true','false') checked,t.parent_id,t.order_id";
			sql = sql + " from sys_power t";
			sql = sql + " LEFT JOIN sys_role_power r ON t.id=r.power_id and r.role_id=?) a";
			sql = sql + " where a.parent_id=? order by a.order_id";
			return Power.dao.find(sql,role_id,parent_menu_id);
		}
	

}
